<?php

declare(strict_types=1);

namespace App\Robot\Amqp\Consumer;


use App\CustHub\Service\LowQueueService;
use App\Robot\Job\ActiveAddFriendJob;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Result;

use PhpAmqpLib\Message\AMQPMessage;

#[Consumer(exchange:"add_friend", routingKey:"add_friend", queue:"add_friend", name:"addFriend", nums:1)]
class ActiveAddFriendConsumer extends ConsumerMessage
{
    public function consumeMessage($data, AMQPMessage $message): Result
    {
        if (empty($data['recordData'])) {
            return Result::DROP;
        }
        // 计算延迟时间
        $greetingInterval = $data['greetInterval'] ?: [
            'min' => 1,
            'max' => 5,
        ];
        $record = $data['recordData'];
        $delay = isset($record['next_add_time']) // 修正变量名为$data
            ? max(strtotime($record['next_add_time']) - time(), 0)
            : rand($greetingInterval['min'] , $greetingInterval['max'] );

        container()->get(LowQueueService::class)->push(new ActiveAddFriendJob($data),$delay);;
        return Result::ACK;
    }

    public function isEnable(): bool
    {
        return \Hyperf\Support\env('AMQP_CONSUMER_ENABLE', false);
    }
}